<?php
/**
 * [LupMisNotAllowedSync]
 * 本代码为系统自动生成代码，请根据自己业务进行修改;
 * 生成时间 2022-02-01 22:12:12;
 * 版权所有 2020-2022 lizhongwen，并保留所有权利;
 * 说明: 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用；不允许对程序代码以任何形式任何目的的再发布;
 * 作者: 中文Lee;
 * 作者主页: http://www.lizhongwen.com;
 * 邮箱: 360811363@qq.com;
 * QQ: 360811363;
 */
namespace application\core;

use Exception;

class Filter
{

    public static $controller;

    public static $action;

    public static $filter;

    public static $controllerPath;


    /**
     * @throws Exception
     */
    public function run($controllerName, $actionName, $pars)
    {
        self::$controllerPath = $this->setControllerPath($controllerName);
        self::$controller = $controllerName;
        self::$action = $actionName;
        $modulesName = MODULES_NAME;
        if (! $modulesName[RUN_PATH]) {
            throw new Exception(RUN_PATH . '业务不存在');
        } else {
            if (! $modulesName[RUN_PATH]['status']) {
                throw new Exception(RUN_PATH . '业务已经停用');
            }
            if ($modulesName[RUN_PATH]['filter']) {
                $filename = APP_PATH . 'application/filter/' . ucfirst(RUN_PATH) . 'Filter.php';
                if (file_exists($filename)) {
                    $filter = '\\application\\filter\\' . ucfirst(RUN_PATH)."Filter";
                    self::$filter = new $filter();
                    self::$filter->run($controllerName, $actionName, $pars);
                } else {
                    throw new Exception(RUN_PATH . '缺少过滤器');
                }
            }

            // 加载公用common.php
            $commonFile = sprintf('%s'.'modules/%s/common.php',APP_PATH,RUN_PATH);
            if (file_exists($commonFile)) {
                include "$commonFile";
            }
            $commonRootFile = sprintf('%s'.'application/core/common.php',APP_PATH);
            if (file_exists($commonRootFile)) {
                include "$commonRootFile";
            }

        }
    }

    private function setControllerPath($controllerName): string
    {
        return 'modules\\' . strtolower(RUN_PATH) . '\\controller\\' . ucfirst($controllerName)."Controller";
    }
}